//
//  NSDictionary+RWAdditions.m
//  Alumna
//
//  Created by ray on 2021/11/17.
//

#import "NSDictionary+RWAdditions.h"

@implementation NSDictionary (RWAdditions)

#pragma mark ***********************************  安全防护  ***********************************

- (NSString *)rw_stringForKey:(id)key {
    
    NSString *def = @"";
    if (!key) return def;
    id value = [self objectForKey:key];
    if (value == nil || value == [NSNull null]) return def;
    if ([value isKindOfClass:[NSString class]]) return (NSString*)value;
    if ([value isKindOfClass:[NSNumber class]]) return [value stringValue];
    return def;
}

- (NSNumber *)rw_numberForKey:(id)key {
    
    NSNumber *def = @0;
    if (!key) return def;
    id value = [self objectForKey:key];
    if (value == nil || value == [NSNull null]) return def;
    if ([value isKindOfClass:[NSNumber class]]) return (NSNumber*)value;
    if ([value isKindOfClass:[NSString class]]) {
        NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
        [f setNumberStyle:NSNumberFormatterDecimalStyle];
        return [f numberFromString:(NSString*)value];
    }
    return def;
}

- (NSArray *)rw_arrayForKey:(id)key {
    
    NSArray *def = @[];
    if (!key) return def;
    id value = [self objectForKey:key];
    if (value == nil || value == [NSNull null]) return def;
    if ([value isKindOfClass:[NSArray class]]) return value;
    return def;
}

- (NSDictionary *)rw_dictionaryForKey:(id)key {
    
    NSDictionary *def = @{};
    if (!key) return def;
    id value = [self objectForKey:key];
    if (value == nil || value == [NSNull null]) return def;
    if ([value isKindOfClass:[NSDictionary class]]) return value;
    return def;
}

- (NSInteger)rw_integerForKey:(id)key {
    
    NSInteger def = 0;
    if (!key) return def;
    id value = [self objectForKey:key];
    if (value == nil || value == [NSNull null]) return def;
    if ([value isKindOfClass:[NSNumber class]]) return [value integerValue];
    if ([value isKindOfClass:[NSString class]]) return [value integerValue];
    return def;
}

- (BOOL)rw_boolForKey:(id)key {
    
    BOOL def = NO;
    if (!key) return def;
    id value = [self objectForKey:key];
    if (value == nil || value == [NSNull null]) return def;
    if ([value isKindOfClass:[NSNumber class]]) return [value boolValue];
    if ([value isKindOfClass:[NSString class]]) return [value boolValue];
    return NO;
}

- (CGFloat)rw_floatForKey:(id)key {
    
    CGFloat def = 0.0;
    if (!key) return def;
    id value = [self objectForKey:key];
    if (value == nil || value == [NSNull null]) return def;
    if ([value isKindOfClass:[NSNumber class]]) return [value floatValue];
    if ([value isKindOfClass:[NSString class]]) return [value floatValue];
    return def;
}

- (double)rw_doubleForKey:(id)key {
    
    double def = 0.0;
    if (!key) return def;
    id value = [self objectForKey:key];
    if (value == nil || value == [NSNull null]) return def;
    if ([value isKindOfClass:[NSNumber class]]) return [value doubleValue];
    if ([value isKindOfClass:[NSString class]]) return [value doubleValue];
    return def;
}

- (long long)rw_longLongForKey:(id)key {
    
    long long def = 0;
    if (!key) return def;
    id value = [self objectForKey:key];
    if (value == nil || value == [NSNull null]) return def;
    if ([value isKindOfClass:[NSNumber class]]) return [value longLongValue];
    if ([value isKindOfClass:[NSString class]]) return [value longLongValue];
    return def;
}

- (NSDictionary *)rw_filterValidValue {
    NSMutableDictionary *muDic = [[NSMutableDictionary alloc] init];
    
    for (NSString *key in self.allKeys) {
        
        id value = [self objectForKey:key];
        
        if (value && ![value isKindOfClass:[NSNull class]]) {
            [muDic setValue:value forKey:key];
        }
    }
    return muDic.copy;
}

@end
